﻿namespace MEF_Test.Bricks
{
    using System.Collections.ObjectModel;
    using System.Windows;

    using MEF_Test.Contracts;

    public abstract class MediaBrowsingProviderBase : DependencyObject
    {
        
        protected IBrowserNodeViewModel _currentElement;
        /// <summary>
        /// Navigates to a new node
        /// </summary>
        /// <param name="newNode">The node to navigate to</param>
        public abstract void GoToNode(IBrowserNodeViewModel newNode);

        public abstract void GoToParentNode(IBrowserNodeViewModel newNode);
        public void GoToParentNode()
        {
            GoToParentNode(_currentElement);
        }
        
        public delegate void CurrentChildrenChangedEventHandler(object sender, CurrentChildrenChangedEventArgs e);
        /// <summary>
        /// Raised when the the current childrens list has completed a modification.
        /// </summary>
        public event CurrentChildrenChangedEventHandler CurrentChildrenChanged;

        /// <summary>
        /// Occurs when the CurrentChildrenChanged event is raised.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void onCurrentChildrenChanged(object sender, CurrentChildrenChangedEventArgs e)
        {
            if (CurrentChildrenChanged != null)
            {
                CurrentChildrenChanged(sender, e);
            }
        }



        public virtual bool IsRoot { get; set; }


        /// <summary>
        /// Internal use only : do not call this method directly : register to the CurrentChildrenChanged instead.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        protected abstract ObservableCollection<IBrowserNodeViewModel> getChildren(IBrowserNodeViewModel node);

        /// <summary>
        /// Force to refresh the children 
        /// <remarks>Typically used when the BrowsingProvider has just been created, as it didn't have the chance to handle any previous event ant therefore, doesn't have any children yet.</remarks>
        /// </summary>
        public void InvalidateChildren()
        {
            onCurrentChildrenChanged(this,new CurrentChildrenChangedEventArgs {Children = getChildren(_currentElement)});
        }
    }

  


}
